package org.zt.java.dal.loadbalance.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zt.java.dal.config.DalConfig;
import org.zt.java.dal.datasource.DynamicDataSource;
import org.zt.java.dal.loadbalance.SlaveLoadBalance;


public class ConsistentHashSlaveLoadBalance implements SlaveLoadBalance{

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	private DalConfig dalConfig;

	public ConsistentHashSlaveLoadBalance(DalConfig dalConfig) {
		this.dalConfig = dalConfig;
	}

	@Override
	public String selectSlaveDataSourceKey() {
		int size = dalConfig.getDataSourceConfig()
				.getSlaveDataSourceConfigs().size();
		int threadHashCode = (int) Thread.currentThread().getId() % size;
		logger.debug(">>>ConsistentHashSlaveLoadBalance,slave size={},selected index:{}",size,threadHashCode);
		return DynamicDataSource.SLAVE_PREFIX + threadHashCode;
	}


}
